package ltd.sshine.networkhelper.autoconfigure;

import com.fasterxml.jackson.databind.ObjectMapper;
import ltd.sshine.networkclient.runner.ClientRunner;
import ltd.sshine.networkclient.service.ProxyService;
import ltd.sshine.networkclient.service.impl.ProxyServiceImpl;
import ltd.sshine.networkclient.utils.ClientUtils;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableConfigurationProperties(NetworkHelperProperties.class)
@ConditionalOnClass(ClientRunner.class)
public class ClientAutoConfiguration {

    private final NetworkHelperProperties networkHelperProperties;

    public ClientAutoConfiguration(NetworkHelperProperties networkHelperProperties) {
        this.networkHelperProperties = networkHelperProperties;
    }

    @Bean
    @ConditionalOnMissingBean(ObjectMapper.class)
    public ObjectMapper objectMapper() {
        return new ObjectMapper();
    }

    @Bean
    public ProxyService proxyService() {
        return new ProxyServiceImpl();
    }

    @Bean
    public ClientRunner clientRunner() {
        ClientUtils clientUtils = new ClientUtils(
                objectMapper(),
                networkHelperProperties.getServer(),
                networkHelperProperties.getClient(),
                proxyService());
        return new ClientRunner(objectMapper(), networkHelperProperties.getServer(), clientUtils);
    }

}
